package com.qiguliuxing.dts.admin.controller;

import com.github.pagehelper.PageInfo;
import com.qiguliuxing.dts.admin.annotation.RequiresPermissionsDesc;
import com.qiguliuxing.dts.common.constant.ApiAdminConsts;
import com.qiguliuxing.dts.common.constant.LogParamsConsts;
import com.qiguliuxing.dts.common.util.ResponseUtils;
import com.qiguliuxing.dts.core.annotation.aoplog.WebLog;
import com.qiguliuxing.dts.db.bean.DtsMerchantApplyVo;
import com.qiguliuxing.dts.db.bean.MerchantApproveParam;
import com.qiguliuxing.dts.service.base.DtsMerchantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Validated
@RestController
@RequestMapping("/admin/merchant")
@Api(tags = ApiAdminConsts.Merchant.PARENT)
@Slf4j
public class AdminMerchantController {

	@Autowired
	private DtsMerchantService dtsMerchantService;

    @GetMapping("/list")
	@RequiresPermissions("admin:merchant:list")
	@RequiresPermissionsDesc(menu = { "系统管理", "商户申请审核" }, button = "查询")
    @ApiOperation(value = ApiAdminConsts.Merchant.LIST)
    @WebLog(description = ApiAdminConsts.Merchant.LIST, excludes = {LogParamsConsts.RESPONSE})
	public Object list(@ApiParam(value = "商户名") String merchantName,
					   @ApiParam(value = "店铺名") String shopName,
					   @ApiParam(value = "审批状态") Integer approveStatus,
					   @ApiParam(value = "页码",defaultValue = "1") @RequestParam(defaultValue = "1") Integer page,
					   @ApiParam(value = "分页大小",defaultValue = "10") @RequestParam(defaultValue = "20") Integer limit) {
		List<DtsMerchantApplyVo> adList = dtsMerchantService.queryMerchantApply(merchantName, shopName, approveStatus, page, limit);
		long total = PageInfo.of(adList).getTotal();
		Map<String, Object> data = new HashMap<>(2);
		data.put("total", total);
		data.put("items", adList);
		return ResponseUtils.ok(data);
	}

	@PostMapping("/verify")
	@RequiresPermissions("admin:merchant:verify")
	@RequiresPermissionsDesc(menu = { "系统管理", "商户申请审核" }, button = "审核")
	@ApiOperation(value = ApiAdminConsts.Merchant.APPROVE)
    @WebLog(description = ApiAdminConsts.Merchant.APPROVE, persistence = true)
	public Object update(@RequestBody MerchantApproveParam approve) {
		dtsMerchantService.approveMerchantApply(approve);
		return ResponseUtils.ok();
	}

}
